import torch

def f(x):
    return (x - 2) ** 2


x_param = torch.nn.Parameter(torch.tensor([-3.5]),requires_grad=True)
optimizer = torch.optim.SGD([x_param],lr=0.1)
for epoch in range(600):
    optimizer.zero_grad()
    loss = f(x_param)
    loss.backward()
    optimizer.step()

print(x_param.data)